/**
 * Created by Mark_ren on 2017/4/6.
 * 苟富贵,無相忘
 */
var express = require('express');
var router = express.Router();
var moment = require("moment");
require('./uuid');

var co = require('co');
var seqModule = require('./createModules');

var seq = new seqModule();


//添加新用户
router.get('/addSweepUser', function(req, res, next) {
    co(function () {
        req.query.id=Math.uuid();
        req.query.createTime=moment().format("YYYY-MM-DD HH:mm:ss");
        seq.sweepUserModule.create(req.query).then(function(results){
            res.json({"success":true,"result":results});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'添加失败'});
        console.log(e);
    })
});


//更新用户总分
router.get('/updSweepUserScore', function(req, res, next) {
    co(function () {
        var id= req.query.userId;
        var score ="0";

        if(req.query.type!="19"){
            score="score+"+parseInt(req.query.score);
            if(req.query.score==null){
                score="score+"+0;
            }
        }
        seq.sequelize.query(" update sweep_user set score= "+score+" where id = '"+id+"'").spread(function(result){
            res.json({"success":true,"result":result});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'修改失败'});
        console.log(e);
    })
});
//查询用户个人信息（主要用来查询积分）
router.get('/getSweepUser', function(req, res, next) {
    co(function () {
        var params={};
        var paramT={};
        var paramU={};
        if(req.query.userId != undefined){
            params["id"]=req.query.userId;
        }
        paramT["where"]=params;
        paramT["order"]=[["createTime",'DESC']];
        seq.sweepUserModule.findAndCountAll(paramT).then(function(result){
            res.json({"success":true,"msg":"查询成功","total":result.count,"result":result.rows});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});

//添加积分记录
router.get('/addSweepScoreLog', function(req, res, next) {
    co(function () {
        req.query.id=Math.uuid();
        req.query.createTime=moment().format("YYYY-MM-DD HH:mm:ss");
        seq.sweepScoreLogModule.create(req.query).then(function(results){
            res.json({"success":true,"result":results});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'添加失败'});
        console.log(e);
    })
});

//查询积分记录
router.get('/getSweepScoreLog', function(req, res, next) {
    co(function () {
        var params={};
        var paramT={};
        var paramU={};
        var createTime10=moment().subtract(10,'minute').format('YYYY-MM-DD HH:mm:ss');
        if(req.query.id != undefined){
            params["id"]=req.query.id;
        }
        if(req.query.userId != undefined){
            params["userId"]=req.query.userId;
        }
        if(req.query.type=="4.1"||req.query.type=="4.2"||req.query.type=="4.3"||req.query.type=="4.4"||req.query.type=="4.5"){
            params["type"]={"$like":"4.%"};
        }else if(req.query.type!=undefined){
            params["type"]=req.query.type;
        }
        if(req.query.type=="10"){
            var paramU = {"createTime":{"$gte":createTime10}};
            params["$and"]=paramU;
        }
        paramT["where"]=params;
        paramT["order"]=[["createTime",'DESC']];
        seq.sweepScoreLogModule.findAndCountAll(paramT).then(function(result){
            res.json({"success":true,"msg":"查询成功","total":result.count,"result":result.rows});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});

//获取任务进度
router.get('/getUserGroupByType', function(req, res, next) {
    co(function () {
        var userId= req.query.userId;
        seq.sequelize.query("select  user_id as userId,type, sum(score) as scoreCount,max(create_time) as createTime" +
            " from sweep_score_log where user_id  = '"+userId+"'  group by type;").spread(function(result){
            res.json({"success":true,"result":result});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});



module.exports = router;